//
//  STInputView.m
//  CuiBoStore
//
//  Created by 崔博 on 16/12/1.
//  Copyright © 2016年 崔博. All rights reserved.
//

#import "STInputView.h"
#import "STNextViewController.h"
#import "STLoginController.h"
@interface STInputView()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *tostLabel;// 提示label
@property (nonatomic, strong) UITextField *userNameFiled;// 账号
@property (nonatomic, strong) UITextField *passWordField;// 密码
@property (nonatomic, strong) UIButton *nextBtn;// 密码
@property (nonatomic, strong) UIButton *loginBtn; // 登录

@end

@implementation STInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
     
        [self addSubviews];
    }
    return self;
}
#pragma mark - delegate
// 输入框代理
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.userNameFiled && range.location > 10) {
        return NO;
    }
    if (textField == self.passWordField) {
        if (range.location > 4) {
            self.nextBtn.backgroundColor = [UIColor redColor];
        }else
        {
            self.nextBtn.backgroundColor = RGB(229, 229, 229);
        }
    }
    return YES;
}
#pragma mark - methoed
- (void)addSubviews
{
    [self addSubview:self.tostLabel];
    [self addSubview:self.userNameFiled];
    [self addSubview:self.passWordField];
    [self addSubview:self.loginBtn];
    [self addSubview:self.nextBtn];
    
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.equalTo(@11);
    }];
    [_userNameFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tostLabel.mas_bottom).offset(11);
        make.height.equalTo(@44);
        make.left.equalTo(self);
        make.right.equalTo(self);
    }];
    [_passWordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userNameFiled.mas_bottom).offset(1);
        make.height.equalTo(@44);
        make.left.equalTo(self);
        make.right.equalTo(self);
    }];
    
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_passWordField.mas_bottom).offset(15);
        make.left.equalTo(@16);
        make.right.equalTo(@(-16));
        make.height.equalTo(@35);
    }];
    
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nextBtn.mas_bottom).offset(22);
        make.right.equalTo(@(-15));
        make.height.equalTo(@17);
        make.bottom.equalTo(@(-10));
    }];

}
#pragma mark - event
// 登录
- (void)loginBtn:(UIButton *)sender{
    self.block(sender,self.loginModel);
}
// 去登录
- (void)goRegis{
    STLoginController *login = [[STLoginController alloc] initWithVCStyle:LoginStyle];
    [self.viewController.navigationController pushViewController:login animated:YES];
}
// 输入框
- (void)userAndPsw:(UITextField *)sender
{
    if (sender == self.userNameFiled) {
        self.loginModel.userName = sender.text;
    }else
    {
        self.loginModel.pwd = sender.text;
    }
}
#pragma mark - set
// 修改按钮文字
- (void)setBtn_Title:(NSString *)btn_Title
{
    _btn_Title = btn_Title;
    [self.nextBtn setTitle:btn_Title forState:UIControlStateNormal];
}
// 去登陆隐藏
- (void)setIsHiddedLogin:(BOOL)isHiddedLogin
{
    _isHiddedLogin = isHiddedLogin;
    self.loginBtn.hidden = isHiddedLogin;
}
#pragma mark - lazy
- (UILabel *)tostLabel
{
    if (!_tostLabel) {
        _tostLabel = [STUIKitTools lableFont:14 textColor:[UIColor lightGrayColor]  text:@"请输入手机号码注册新用户"];
    }
    return _tostLabel;
}

- (UITextField *)userNameFiled
{
    if (!_userNameFiled) {
        _userNameFiled = [STUIKitTools textFileHolder:@"  请输入手机号码" style:UITextBorderStyleNone];
        [_userNameFiled addTarget:self action:@selector(userAndPsw:) forControlEvents:UIControlEventEditingChanged];
        _userNameFiled.backgroundColor = [UIColor whiteColor];
        _userNameFiled.delegate = self;
    }
    return _userNameFiled;
}

- (UITextField *)passWordField
{
    if (!_passWordField) {
        _passWordField = [STUIKitTools textFileHolder:@"  请输入密码" style:UITextBorderStyleNone];
        [_passWordField addTarget:self action:@selector(userAndPsw:) forControlEvents:UIControlEventEditingChanged];
        _passWordField.secureTextEntry = YES;
        _passWordField.backgroundColor = [UIColor whiteColor];
        _passWordField.delegate = self;
    }
    return _passWordField;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [STUIKitTools buttonTitle:@"去登录" font:14 action:@selector(goRegis) titleColor:RGB(59,165, 241) target:self];
        _loginBtn.backgroundColor = [UIColor clearColor];
    }
    return _loginBtn;
}
- (UIButton *)nextBtn
{
    if (!_nextBtn) {
        _nextBtn = [STUIKitTools buttonTitle:@"下一步" font:14 action:@selector(loginBtn:) titleColor:RGB(132, 132, 132) target:self];
        _nextBtn.layer.cornerRadius = 5;
        _nextBtn.layer.masksToBounds = YES;
        _nextBtn.backgroundColor = RGB(229, 229, 229);
    }
    return _nextBtn;
}
- (STLgoinModel *)loginModel
{
    if (!_loginModel) {
        _loginModel = [[STLgoinModel alloc] init];
    }
    return _loginModel;
}
@end
